﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MLY.Reports
{
    public partial class reportsParams : MLY.Bll.BasePage
    {
        private string userRole = "";
        private MLY.Bll.ReportInfo repInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                if (int.Parse(ddlReports.SelectedValue.ToString()) == 2) // rptOpenLateOrders
                {
                    lblErr.Visible = false;
                    this.panelErr.Visible = false;
                    lblOrg.Visible = true;
                    ddlOrgs.Visible = true;
                    columnListDdl.Visible = false;
                    columnListDdlValidator.Visible = false;
                    lblBetween2.Visible = lblBetween.Visible = false;
                    this.ddlLateOrderColumnDate.Visible = true;
                }
                else if (int.Parse(ddlReports.SelectedValue.ToString()) == 4) // rptWorkOrdersByDates
                {
                    lblOrg.Visible = true;
                    ddlOrgs.Visible = true;
                    columnListDdl.Visible = true;
                    columnListDdlValidator.Visible = true;
                    lblBetween.Visible = true;
                    lblBetween2.Visible = true;
                    this.ddlLateOrderColumnDate.Visible = false;
                }
                else if (int.Parse(ddlReports.SelectedValue.ToString()) == 5) // rptErrLog
                {
                    lblOrg.Visible = false;
                    ddlOrgs.Visible = false;
                    columnListDdl.Visible = false;
                    columnListDdlValidator.Visible = false;
                    lblBetween.Visible = true;
                    lblBetween2.Visible = true;
                    this.ddlLateOrderColumnDate.Visible = false;
                }
                else if (int.Parse(ddlReports.SelectedValue.ToString()) == 6) // rptCanceledWorkOrders
                {
                    lblErr.Visible = false;
                    this.panelErr.Visible = false;
                    lblBetween2.Visible = lblBetween.Visible = false;
                    lblOrg.Visible = false;
                    ddlOrgs.Visible = false;
                    columnListDdl.Visible = false;
                    columnListDdlValidator.Visible = false;
                     this.ddlLateOrderColumnDate.Visible = false;
                }
                else
                {
                    lblErr.Visible = false;
                    this.panelErr.Visible = false;
                    lblOrg.Visible = true;
                    ddlOrgs.Visible = true;
                    lblBetween2.Visible = lblBetween.Visible = false;
                    columnListDdl.Visible = false;
                    columnListDdlValidator.Visible = false;
                    this.ddlLateOrderColumnDate.Visible = false;
                }
            }
            if (!IsPostBack)
            {
                userRole = SessParam.IsClient ? "Client" : "Contructor";

                //WorkOrderByDatesReportLink.NavigateUrl = "~/Reports/DataReports/WorkOrderByDateReport.aspx?userRole=" + userRole;
                //InventoryReportLink.NavigateUrl = "~/Reports/DataReports/OpenOrdersInventoryReport.aspx?userRole=" + userRole;
                //OpenLateWorkOrdersReport.NavigateUrl = "~/Reports/DataReports/LateOrdersReport.aspx?userRole=" + userRole;
                //DataFlowReportLink.NavigateUrl = "~/Reports/ChartReports/DataFlowReport.aspx?userRole=" + userRole;
                //DeadInventoryReportLink.NavigateUrl = "~/Reports/SCCoverReports/DeadInventoryReport.aspx?userRole=" + userRole;
            }

            /// DEMO CODE
            if (Session["UserId"].ToString() == "39")
            {
                if ((string)Session["DemoStage"] == "Reports")
                    MLY.Bll.Demo.MoveToNextDemoSubStage();
                
                WorkOrderByDatesReportLink.Attributes.Add("onmouseover", "ShowDiv('WorkOrderByDatesReportLinkDiv')");
                WorkOrderByDatesReportLink.Attributes.Add("onmouseout", "HideDiv('WorkOrderByDatesReportLinkDiv')");

                InventoryReportLink.Attributes.Add("onmouseover", "ShowDiv('InventoryReportLinkDiv')");
                InventoryReportLink.Attributes.Add("onmouseout", "HideDiv('InventoryReportLinkDiv')");

                OpenLateWorkOrdersReport.Attributes.Add("onmouseover", "ShowDiv('OpenLateWorkOrdersReportDiv')");
                OpenLateWorkOrdersReport.Attributes.Add("onmouseout", "HideDiv('OpenLateWorkOrdersReportDiv')");

                RelatedOrganizationsSummaryLink.Attributes.Add("onmouseover", "ShowDiv('RelatedOrganizationsSummaryLinkDiv')");
                RelatedOrganizationsSummaryLink.Attributes.Add("onmouseout", "HideDiv('RelatedOrganizationsSummaryLinkDiv')");

                //SystemErrorsReportLink.Attributes.Add("onmouseover", "ShowDiv('SystemErrorsReportLinkDiv')");
                //SystemErrorsReportLink.Attributes.Add("onmouseout", "HideDiv('SystemErrorsReportLinkDiv')");

                CanceledOrdersReportLink.Attributes.Add("onmouseover", "ShowDiv('CanceledOrdersReportLinkDiv')");
                CanceledOrdersReportLink.Attributes.Add("onmouseout", "HideDiv('CanceledOrdersReportLinkDiv')");

                //DataFlowReportLink.Attributes.Add("onmouseover", "ShowDiv('DataFlowReportLinkDiv')");
                //DataFlowReportLink.Attributes.Add("onmouseout", "HideDiv('DataFlowReportLinkDiv')");
            }

            //    if (((string)Session["DemoCode"] == "3") || ((string)Session["DemoCode"] == "4") 
            //        || ((string)Session["DemoCode"] == "4.5"))
            //    {
            //        Session["DemoCode"] = "5";
            //        DataFlowReportLink.CssClass = "floater regFont demohighlight";
            //        DemoStageHiddenField.Value = "4.5";
            //    }
            //    else if (IsPostBack && ((string)Session["DemoCode"] == "6.1"))
            //    {
            //        Session["DemoCode"] = "5";
            //        OpenLateWorkOrdersReport.CssClass = "floater regFont demostophighlight";
            //        DemoStageHiddenField.Value = "";
            //    }
            //    else
            //    {
            //        OpenLateWorkOrdersReport.CssClass = "floater regFont demostophighlight";
            //    }
          
            /// END OF DEMO CODE
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            if ((string)Session["DemoStage"] == "Reports")
            {
                MLY.Bll.Demo.MoveToNextDemoStage();
            }
        }

        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            repInfo = new MLY.Bll.ReportInfo();
            repInfo.ReportType = int.Parse(ddlReports.SelectedValue);
            repInfo.OrganizationId = (int)Session["OrganizationId"];
            repInfo.SelectedOrganizationId = int.Parse(ddlOrgs.SelectedValue);
            switch (repInfo.ReportType)
            {
                case 2:
                    repInfo.DateColumn = this.ddlLateOrderColumnDate.SelectedItem.Text;
                    break;
                case 4:
                    repInfo.DateColumn = columnListDdl.SelectedItem.Text;
                    repInfo.DateFrom = Utils.ParseDate(txtFromDate.Text);
                    repInfo.DateTill = Utils.ParseDate(txtToDate.Text);
                    break;
                case 5:
                    repInfo.DateFrom = Utils.ParseDate(txtFromDate.Text);
                    repInfo.DateTill = Utils.ParseDate(txtToDate.Text);
                    break;
                default:
                    break;
            }
            
            string repKey = MLY.Bll.ReportInfo.AddReportInfo(repInfo);
            string script = String.Format(@"<script language ='javascript'>window.open('reportsViewer.aspx?userRole={0}&repinfo={1}','a{2}','width=950, height=650,scrollbars=yes,resizable=yes,left=20,top=20')</script>", 
                                            Request.QueryString["userRole"].ToString(),repKey,Guid.NewGuid().ToString("N"));
            this.ClientScript.RegisterStartupScript(this.GetType(), "myScript", script);
        }

        protected override void OnError(EventArgs e)
        {
            base.OnError(e);
        }

        protected void btnClose_OnClick(object sender, EventArgs e)
        {
        }

        protected void ddlReports_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        public override void Dispose()
        {
            base.Dispose();
        }

        protected void ddlReports_PreRender(object sender, EventArgs e)
        {
        }

        protected void ddlOrgs_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlOrgs.Items.Clear();
                ListItem all = new ListItem("All", "0");
                ddlOrgs.Items.Add(all);
                ddlOrgs.AppendDataBoundItems = true;
                if ((Request.QueryString["userRole"]!=null) &&
                    (Request.QueryString["userRole"].ToString() == "Client"))
                {
                    ddlOrgs.DataSource = MLY.Bll.clsOrganization.GetContractorsByClientId((int)Session["OrganizationId"]);
                    ddlOrgs.DataBind();
                }
                else
                {
                    ddlOrgs.DataSource = MLY.Bll.clsOrganization.GetClientsByContractorId((int)Session["OrganizationId"]);
                    ddlOrgs.DataBind();
                }
            }
        }

        protected void ddlOrgs_PreRender(object sender, EventArgs e)
        {
        }

        
    }
}
